<?
/************code by seven****************/
require("../../common/init.php");
require("author.php");
require("upfile.php");

$gee_cate = $product_category;
$gee_product = $product;
$gee = $promotion_fullcut;

$url='promotion_discount';
$s=isset($_GET["s"])?clean($_GET["s"]):''; // search
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);

$promotion_id =0;
if(isset($_GET['promotion_id'])) $promotion_id = clean($_GET['promotion_id']); 
$item =  $sales_promotion_list->get_row($promotion_id);
$promotion_name = $item['name_'.$lang];

$cate = $gee_cate->get_row($promotion_id, 'promotion_id');
$product = $gee_product->get_row($promotion_id, 'promotion_id');

$promotion_type = $item['category_id'];

$filter="0";
if(isset($_GET["filter"]))$filter=clean($_GET["filter"]);
if(isset($_POST["filter"]))$filter=clean($_POST["filter"]);

// build category tree 构建分类级别树
$tem = $gee_cate->get_rows(1024,'','','seq_no');
$tree=unlimitedlevel($tem);

$cate_array=array();
foreach($tree as $val){
    $cate_array[$val['id']]=array(
        'id'=>$val['id'],
        'name'=>$val['html_prefix'].$val['name_'.$lang],
    );
}

switch ($filter){
    case 0:
        $title=ROOT_SALES_PROMOTION_DISCOUNT;
        break;
    case 1:
        $title=ROOT_SALES_PROMOTION_UNFINISH;
        break;
    case 2:
        $title = ROOT_SALES_PROMOTION_FINISH;
        break;
    default:
        $title=ROOT_SALES_PROMOTION;
}

// sql action / sql 动作
switch ($action){
    case "update":
        sql_update();
        break;
    case "add":
        sql_insert();
        break;
}
if($id==0){
    $page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
    $order='id';
    $where = array();
    $info = array();
    if($promotion_type==2){
        $info = $gee_cate->get_rows(4096, 'promotion_id', $promotion_id);
    }else{
        $where[]=array('name'=>'promotion_id','val'=>$promotion_id);
        $where[]=array('name'=>'is_enabled','val'=>1);
        $pages=$gee_product->count_pages($where);
        $info=$gee_product->get_page($page,$where,$order);
    }
    $cate_info = $gee_cate->get_rows(4096);
}
else $info=$gee->get_row($id);

function add_cate(){
    global $gee_cate, $url, $id;
    $id = $_POST['id'];
    $discount_type = 1;
    $discount_value = 20;
    $promotion_id = 9;
    $arr=array(
        'discount_type'=>$discount_type,
        'promotion_id' => $promotion_id,
        'discount_value' => $discount_value,
    );
    //pre($arr); exit;
    $gee_cate->update($arr, 'id', $id );
    echo $discount_value;
    exit;
   // wheel("$url.php?promotion_id=".$promotion_id);
}
function add()
{
    global $h, $url, $info, $cate_info, $promotion_id, $promotion_type;
    $row = array();
    echo $h->form($url.'.php', 1, '');
    if($promotion_type == 2){
        show_cate($cate_info, true, true);
    }else{
        show_box($row, true, true);
    }
    echo $h->hidden('action', 'add');
    echo $h->former();
}
function update($info)
{
    global $gee, $h, $url, $id;
    if($id=="")
    {
        echo "Invali argument!!!";
        exit;
    }
    echo $h->form($url.'.php', 1, '');
    show_box($info, true, false);
    echo $h->hidden('action', 'update');
    echo $h->former();
}
function delete($id)
{
    global $gee_cate,$url,$gee_product,$id,$promotion_id,$promotion_type;
    $arr=array(
        'discount_type'=>0,
        'promotion_id' =>0,
        'discount_value' =>0
    );
    $gee=$gee_cate;
    if($promotion_type==1) $gee=$gee_product;
    if($gee->update($arr,'id',$id)){
        wheel("$url.php?promotion_id=".$promotion_id);
    }
}
function show_info($info)
{
    global $id;
    if($id==0)
    {
        show_boxes($info);
    }
    else
    {
        show_box($info, false);
    }
}
function sql_insert()
{
    global $gee,$url,$lang;
    $arr=array(
        'full'=>$_POST["full"],
        'promotion_id'=>$_POST["promotion_id"],
        'cut' => $_POST['cut'],
        'type' => $_POST['type'],
        'user_id'=>decrypt($_COOKIE['userid']),
    );
    //pre($arr); exit;
    if($gee->insert($arr))
        yell(ROOT_ADD_SUCCESS,$url.'.php');
}
function sql_update()
{
    global $gee, $url,$lang;
    $id=$_POST["id"];
    $arr=array(
        'full'=>$_POST["full"],
        'promotion_id' => $_POST['promotion_id'],
        'cut' => $_POST['cut'],
        'type' => $_POST['type'],
        'user_id'=>decrypt($_COOKIE['userid']),
    );
    //pre($arr); exit;
    $gee->update($arr, 'id', $id );
    yell(ROOT_UPDATE_SUCCESS,$url.'.php?promotion_id='.$_POST['promotion_id']);

}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end 